/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Dawn
 */
@WebServlet(name="OnlineFriend", urlPatterns={"/OnlineFriend"})
public class OnlineFriend extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        ResultSet rst=null;
        Statement stm=null;
        
        try {
            /*TODO output your page here
            out.println("<html>");*/
            //out.println("<head>");
            //out.println("<script type='text/javascript' src='javascript/chatmanage.js'></script>");
            //out.println("<script type='text/javascript' src='javascript/PageRefresh.js'></script>");
            //out.println("<title>Servlet OnlineFriend</title>");
            //out.println("</head>");
            /*out.println("<body>");
            out.println("<h1>Servlet OnlineFriend at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
            Connection connection = null;

            try{
                try {
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                } catch (InstantiationException ex) {
                    Logger.getLogger(LoginProcess.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(LoginProcess.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            catch (ClassNotFoundException ex){
            }

            connection = DriverManager.getConnection("jdbc:mysql://localhost/chatter?user=root&password=");

            stm=connection.createStatement();
            if (stm.execute("select onlineuser.Username, Role from users, onlineuser, (select Friend from friend where Username = '" + request.getParameter("Username") + "') as res where onlineuser.username = Friend AND onlineuser.username = users.username"))
            {
                rst=stm.getResultSet();
                while (rst.next())
                {
                    if (rst.getString(2).equalsIgnoreCase("User"))
                    {
                        out.println("<a onclick=chatWithOtherUser('" + request.getParameter("Username") +"','" + rst.getString(1) + "'); href=#>" + rst.getString(1) + "</a>");
                    }
                    else if (rst.getString(2).equalsIgnoreCase("Moderate"))
                    {
                        out.println("<a class='moderate' onclick=chatWithOtherUser('" + request.getParameter("Username") +"','" + rst.getString(1) + "'); href=#>" + rst.getString(1) + "</a>");
                    }
                }
            }

        } finally {
            rst.close();
            stm.close();
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(OnlineFriend.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(OnlineFriend.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
